package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.system.domain.GoodsSignin;
import com.qinggeng.project.system.domain.UserGift;
import com.qinggeng.project.system.mapper.UserGiftMapper;
import com.qinggeng.project.system.service.IGoodsSigninService;
import com.qinggeng.project.system.service.IUserGiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户礼品Service业务层处理
 *
 * @author qinggeng
 * @date 2020-07-15
 */
@Service
public class UserGiftServiceImpl implements IUserGiftService
{
    @Resource
    private UserGiftMapper userGiftMapper;

    @Autowired
    private IGoodsSigninService goodsSigninService;

    /**
     * 查询用户礼品
     *
     * @param userGiftId 用户礼品ID
     * @return 用户礼品
     */
    @Override
    public UserGift selectUserGiftById(Long userGiftId)
    {
        return userGiftMapper.selectUserGiftById(userGiftId);
    }

    /**
     * 查询用户礼品列表
     *
     * @param userGift 用户礼品
     * @return 用户礼品
     */
    @Override
    public List<UserGift> selectUserGiftList(UserGift userGift)
    {
        return userGiftMapper.selectUserGiftList(userGift);
    }

    /**
     * 新增用户礼品
     *
     * @param userGift 用户礼品
     * @return 结果
     */
    @Override
    public int insertUserGift(UserGift userGift)
    {
        userGift.setCreateTime(DateUtils.getNowDate());
        return userGiftMapper.insertUserGift(userGift);
    }

    /**
     * 修改用户礼品
     *
     * @param userGift 用户礼品
     * @return 结果
     */
    @Override
    public int updateUserGift(UserGift userGift)
    {
        userGift.setUpdateTime(DateUtils.getNowDate());
        return userGiftMapper.updateUserGift(userGift);
    }

    /**
     * 批量删除用户礼品
     *
     * @param userGiftIds 需要删除的用户礼品ID
     * @return 结果
     */
    @Override
    public int deleteUserGiftByIds(Long[] userGiftIds)
    {
        return userGiftMapper.deleteUserGiftByIds(userGiftIds);
    }

    /**
     * 删除用户礼品信息
     *
     * @param userGiftId 用户礼品ID
     * @return 结果
     */
    @Override
    public int deleteUserGiftById(Long userGiftId)
    {
        return userGiftMapper.deleteUserGiftById(userGiftId);
    }

    @Override
    public UserGift selectUserGiftByUserId(String giftType, String createTime, Long userId) {
        return userGiftMapper.selectUserGiftByUserId(giftType,createTime,userId);
    }

    @Override
    public int addSignin(Long signinId, Long userId) {
        GoodsSignin goodsSignin = goodsSigninService.selectGoodsSigninById(signinId);
        UserGift userGift = new UserGift();
        userGift.setGiftType(CommonEnum.STATUS_10.getValue());
        userGift.setGoodsName(goodsSignin.getGoodsName());
        userGift.setGoodsImg(goodsSignin.getGoodsImg());
        userGift.setUserId(userId);
        return insertUserGift(userGift);
    }

    @Transactional
    @Override
    public int addGift(Long userId) {
        GoodsSignin where = new GoodsSignin();
        where.setIndexType(0);
        List<GoodsSignin> goodsSigninList = goodsSigninService.selectGoodsSigninList(where);
        UserGift userGift = null;
        for (GoodsSignin goodsSignin : goodsSigninList) {
            userGift = new UserGift();
            userGift.setUserId(userId);
            userGift.setGiftType(CommonEnum.STATUS_20.getValue());
            userGift.setGoodsName(goodsSignin.getGoodsName());
            userGift.setGoodsImg(goodsSignin.getGoodsImg());
            insertUserGift(userGift);
        }
        return 1;
    }
}
